home *** CD-ROM | disk | FTP | other *** search
- Path: news-m01.ny.us.ibm.net!usenet
- From: rwolf@ibm.net
- Newsgroups: comp.lang.c++,rb.technical
- Subject: Re: Can copy constructor and operator= share code?
- Date: 4 Mar 1996 02:26:59 GMT
- Organization: Rudolph Research
- Message-ID: <4hdkdj$3id0@news-s01.ny.us.ibm.net>
- References: <4h2kcn$40d@rap.SanDiegoCA.ATTGIS.COM> <VA.00000053.00cdab05@fred>
- Reply-To: rwolf@ibm.net
- NNTP-Posting-Host: slip166-72-132-106.tx.us.ibm.net
- X-Newsreader: IBM NewsReader/2 v1.2
-
- In <VA.00000053.00cdab05@fred>, Frederic LACHASSE <lachass@worldnet.fr> writes:
- >In article <4h2kcn$40d@rap.SanDiegoCA.ATTGIS.COM>, borisb@sd.znet.com
- >(Boris Burtin) wrote:
- >>
- >> I have noticed that a copy constructor and operator= perform pretty
- >> much the same function. The code I wrote for my class simply copies
- >> each member variable from one class to the other.
- >>
-
- A rather typical form is to have the create a "ShallowCopy"
- function that is called from both the copy constructor and the
- operator=. ShallowCopy copies only the member variables
- at the current derivation level.
-
- T::T(const T& t)
- {
- ShallowCopy(t);
- }
-
- T& operator=(const T& t )
- {
- if (&t!=this)
- {
- ClassReset(); // direct destructor calls are bad form
- ShallowCopy(t);
- parentclass::operator=(t);
-
- }
- return *this;
- }
-
-
- If this form is followed at each level of derivation it will do what you want.
-